package si.yun.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import si.yun.dao.TeacherDao;
import si.yun.entity.*;
import si.yun.service.TeacherSerivce;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName TeaSettingSerivceImpl
 * @Author SXM
 * @Date 2021/07/29 14:33
 * @Version 1.0
 */
@Service
@PropertySource({"config.properties"})
public class TeacherSerivceImpl implements TeacherSerivce {
    @Autowired
    private TeacherDao dao;

    /*
     * @Author SXM
     * @Description //查询获取课程分类
     * @return java.util.List<si.yun.entity.ztreeNode>
     * @Date 2021/8/2  15:49
     **/
    @Override
    @Transactional
    public List<ztreeNode> selCourseClass() {
        List<ztreeNode> tree = dao.selCourseClass();
        if(tree!=null) {
            for(ztreeNode tree1:tree) {
                int childid = tree1.getNodeid();
                List<ztreeNode> tree2 = selCourseClass2(childid);
                tree1.setChildren(tree2);
            }
            return tree;
        }
        return null;
    }

    /*
     * @Author SXM
     * @Description //查询获取课程分类--子节点
     * @return java.util.List<si.yun.entity.ztreeNode>
     * @Date 2021/8/2  16:56
     **/
    @Transactional
    public List<ztreeNode> selCourseClass2(int id){
        List<ztreeNode> children = dao.selCourseClass2(id);
        if(children!=null) {
            for(ztreeNode tree1:children) {
                int childid = tree1.getNodeid();
                List<ztreeNode> tree2 = selCourseClass2(childid);
                tree1.setChildren(tree2);
            }
            return children;
        }
        return null;
    }

    /*
     * @Author SXM
     * @Description //表格上方按钮中的总和数
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @Date 2021/8/4  20:07
     **/
    @Override
    public Map<String,Object> selCourseSum(Integer uId) {
        Map<String,Object> map=new HashMap<>();
        //查总数
        int allSum=dao.selCourseAllSum(uId);
        //查已上架总数
        int status2Sum=dao.selCourseStatus2Sum(uId);
        //查未上架总数
        int status3Sum=dao.selCourseStatus3Sum(uId);
        //查待审核总数
        int cstatus3Sum=dao.selCourseCstatus3Sum(uId);
        //查未通过总数
        int cstatus5Sum=dao.selCourseCstatus5Sum(uId);
        map.put("allSum",allSum);
        map.put("status2Sum",status2Sum);
        map.put("status3Sum",status3Sum);
        map.put("cstatus3Sum",cstatus3Sum);
        map.put("cstatus5Sum",cstatus5Sum);
        return map;
    }

    /*
     * @Author SXM
     * @Description //查询个人课程表--全部课程表、是否上架、是否审核通过
     * @return java.util.List<si.yun.entity.SyCourseTab>
     * @Date 2021/8/4  16:33
     **/
    @Override
    @Transactional
    public List<SyCourseTab> selcourseTab(CourseTab tab) {
        List<SyCourseTab> list=dao.selcourseTab(tab);
        if(list!=null) {
            for(SyCourseTab tab1:list) {
                String className = selcourseTabClass(tab1.getClassId());
                tab1.setClassName(className);
            }
        }
        return list;
    }

    /*
     * @Author SXM
     * @Description //个人课程表--查询对应分类名称
     * @return java.lang.String
     * @Date 2021/8/4  14:44
     **/
    @Transactional
    public String selcourseTabClass(Integer classId){
        SyCourseClass courseClass= dao.selcourseTabClass(classId);
        String className=courseClass.getName();
        if(courseClass.getParentId()!=0) {
            String name=selcourseTabClass(courseClass.getParentId());
            return name+className;
        }
        return className;
    }

    /*
     * @Author SXM
     * @Description //更改个人课程的上下架状态
     * @return int
     * @Date 2021/8/5  11:18
     **/
    @Override
    public int updCourseStatus(int courseId, int status) {
        return dao.updCourseStatus(courseId,status);
    }


    /*
     * @Author SXM
     * @Description //查询课程审核详情
     * @return java.util.List<si.yun.entity.SyCourseRecordTab>
     * @Date 2021/8/7  16:49
     **/
    @Override
    public List<SyCourseRecordTab> selcourseRecord(Integer Id) {
        return dao.selcourseRecord(Id);
    }

    /*
     * @Author SXM
     * @Description //查询单个课程详情
     * @return si.yun.entity.SyCourseTab
     * @Date 2021/8/9  16:13
     **/
    @Override
    @Transactional
    public SyCourseTab selcourse(Integer courseId) {
        SyCourseTab course = dao.selcourse(courseId);
        String className = selcourseTabClass(course.getClassId());
        course.setClassName(className);
        String urlName=cvpath+course.getCourseUrl();
        course.setCourseUrl(urlName);
        return course;
    }

    /*课程视频路径*/
    @Value("${coursevideopath}")
    private String cvpath;

    /*
     * @Author SXM
     * @Description //查询分类全名称
     * @return java.lang.String
     * @Date 2021/8/12  21:26
     **/
    @Override
    public String selcourseClassName(Integer classId){ //重名了
        SyCourseClass courseClass= dao.selcourseClassName(classId);
        String className=courseClass.getName();
        if(courseClass.getParentId()!=0) {
            String name=selcourseTabClass(courseClass.getParentId());
            return name+className;
        }
        return className;
    }

    /*
     * @Author SXM
     * @Description //查询课程的日志
     * @return si.yun.entity.SyCourseTab
     * @Date 2021/8/13  20:36
     **/
    @Override
    public List<SyCourseTab> selJournal(Integer id) {
        List<SyCourseTab> syCourseTab = dao.selJournal(id);
        return syCourseTab;
    }

    /*
     * @Author SXM
     * @Description //修改课程详情
     * @return int
     * @Date 2021/8/15  9:40
     **/
    @Override
    public int updCourseEdit(CourseTab tab) {
        return dao.updCourseEdit(tab);
    }

    /*
     * @Author SXM
     * @Description //删除课程
     * @return int
     * @Date 2021/8/13  21:46
     **/
    @Override
    public int delCourse(Integer id) {
        return dao.delCourse(id);
    }

    /*
     * @Author SXM
     * @Description //添加课程
     * @return int
     * @Date 2021/8/14  21:05
     **/
    @Override
    public int addCourse(SyCourseTab courseTab,Integer uId) {
        return dao.addCourse(courseTab,uId);
    }

    /*
     * @Author SXM
     * @Description //查询微圈表格数据
     * @return java.util.List<si.yun.entity.SyForumTab>
     * @Date 2021/8/17  14:38
     **/
    @Override
    public List<SyForumTab> selForumTab(ForumTab tab) {
        List<SyForumTab> list=dao.selForumTab(tab);
        List<SyForumTab> listn=new ArrayList<SyForumTab>();
        for(SyForumTab tab1:list){
            Integer num = dao.selcollectionNum(tab1.getMicrocircleId());
            tab1.setCollectNum(num);
            if(tab.getCollectNum()!=null){
                if(num>(tab.getCollectNum()-5)&&num<(tab.getCollectNum()+5)){
                    listn.add(tab1);
                }
            }
        }
        return listn.size()>0?listn:list;
    }

    /*
     * @Author SXM
     * @Description //添加微圈文章
     * @return int
     * @Date 2021/8/21  9:18
     **/
    @Override
    public int addMicroCircle(ForumTab tab) {
        return dao.addMicroCircle(tab);
    }

    /*
     * @Author SXM
     * @Description //修改微圈文章
     * @return int
     * @Date 2021/8/21  9:44
     **/
    @Override
    public int updMicroCircle(ForumTab tab) {
        return dao.updMicroCircle(tab);
    }

    /*
     * @Author SXM
     * @Description //删除微圈文章
     * @return int
     * @Date 2021/8/21  11:00
     **/
    @Override
    public int delMicroCircle(Integer id) {
        return dao.delMicroCircle(id);
    }

    /*
     * @Author SXM
     * @Description //查看微圈
     * @return si.yun.entity.SyForumTab
     * @Date 2021/8/21  14:39
     **/
    @Override
    @Transactional
    public SyForumTab selMicroCircle(Integer id) {
        SyForumTab syForumTab = dao.selMicroCircle(id);
        String className=selClassName(syForumTab.getActicleCategoryId());//分类名称
        syForumTab.setClassName(className);
        Integer num = dao.selcollectionNum(syForumTab.getMicrocircleId());//收藏数
        syForumTab.setCollectNum(num);
        return syForumTab;
    }

    /*
     * @Author SXM
     * @Description //微圈管理--发送评论
     * @return int
     * @Date 2021/8/25  10:16
     **/
    @Override
    public int addComment(ForumTab tab) {
        return dao.addComment(tab);
    }

    /*后台用户图片路径*/
    @Value("${backUserImgpath}")
    private String pathh;

    /*前台用户图片路径*/
    @Value("${beforeuserpath}")
    private String pathq;

    /*
     * @Author SXM
     * @Description //微圈管理--查询评论区
     * @return java.util.List<si.yun.entity.SyCommentList>
     * @Date 2021/8/27  10:55
     **/
    @Override
    public List<SyCommentList> selComment(Integer id, Integer limi) {
        List<SyCommentList> list=dao.selComment(id,limi);//查询后台用户
        for(SyCommentList comm:list){
            if(comm.getUserName()!=null){
                if(comm.getUserImg()!=null){
                    comm.setUserImg(pathh+comm.getUserImg());
                }
                continue;
            }else{//查询前台用户
                SyCommentList com = dao.selComment2(comm.getUserId());
                comm.setUserName(com.getUserName());
                comm.setUserImg(pathq+com.getUserImg());
            }
        }
        return list;
    }

    /*
     * @Author SXM
     * @Description //公共课程\微圈管理_添加分类节点
     * @return int
     * @Date 2021/9/2  20:32
     **/
    @Override
    public int addClassTree(Integer parentId, String name,Integer uId) {
        return dao.addClassTree(parentId,name,uId);
    }

    /*
     * @Author SXM
     * @Description //公共课程\微圈管理_修改分类节点
     * @return int
     * @Date 2021/9/3  15:29
     **/
    @Override
    public int updClassTree(Integer id, String name, Integer uId) {
        return dao.updClassTree(id,name,uId);
    }

    /*
     * @Author SXM
     * @Description //公共课程\微圈管理_删除分类节点
     * @return int
     * @Date 2021/9/3  17:03
     **/
    @Override
    public int delClassTree(Integer id) {
        return dao.delClassTree(id);
    }

    /*
     * @Author SXM
     * @Description //微圈管理_隐藏评论
     * @return int
     * @Date 2021/9/6  19:20
     **/
    @Override
    public int updComment(Integer id, Integer uId) {
        return dao.updComment(id,uId);
    }

    /*
     * @Author SXM
     * @Description //微圈管理_删除评论
     * @return int
     * @Date 2021/9/6  19:49
     **/
    @Override
    public int delComment(Integer id) {
        return dao.delComment(id);
    }

    /*
     * @Author SXM
     * @Description //查询分类全名称
     * @return java.lang.String
     * @Date 2021/8/21  20:36
     **/
    @Transactional
    public String selClassName(Integer classId){
        SyCourseClass courseClass= dao.selcourseTabClass(classId);
        String className=courseClass.getName();
        if(courseClass.getParentId()!=0) {
            String name=selcourseTabClass(courseClass.getParentId());
            return name+className;
        }
        return className;
    }
}
